#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = "wqk151"
__name__="sql_helper"
__date__="2016/6/26"
__time__="17:18"
"""

# 此目录存放对数据库的操作
import MySQLdb

"""
# 修改前
class  MySqlHelper(object):
    def __init__(self):
        pass
    def get_dict(self,sql,params):
        conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='123456', db='object')
        cur = conn.cursor()
        reCount = cur.execute(sql,params)
        data = cu.fetchall()
        conn.commit()
        cur.close()
        conn.close()
        return data
    def get_one(self,sql,params):
        conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='123456', db='object')
        cur = conn.cursor()
        reCount = cur.execute(sql,params)
        data = cu.fetchone()
        cur.close()
        conn.close()
        return data
"""

"""
调用不在这里写
helper = MySqlHelper()
sql = "select * form admin where id >%s"
params = (1,)
simple_data = helper.get_one(sql,params)
dict_data = helper.get_dict(sql,params)
"""
# sql_server_helper
# oracle_helper  等等

"""
# 改造一：
class  MySqlHelper(object):
    def __init__(self):
        self.__conn_dict = dict(host='127.0.0.1', user='root', passwd='123456', db='object')
                    # 等同于{'host':'127.0.0.1','user':'root'}
    def get_dict(self,sql,params):
        conn = MySQLdb.connect(**self.__conn_dict)
        cur = conn.cursor()
        reCount = cur.execute(sql,params)
        data = cu.fetchall()
        conn.commit()
        cur.close()
        conn.close()
        return data
    def get_one(self,sql,params):
        conn = MySQLdb.connect(**self.__conn_dict)
        # * list ; ** dict
        # 没有加*，传入的是字典，加*后，传入的是字典的内容'host= ... object'
        cur = conn.cursor()
        reCount = cur.execute(sql,params)
        data = cu.fetchone()
        cur.close()
        conn.close()
        return data
"""

# 改造二： 连接串写入conf.py中
import conf
class  MySqlHelper(object):
    def __init__(self):
        self.__conn_dict = conf.conn_dict   # 配置在conf文件中
        # 类的私有字段
    def get_dict(self,sql,params):
        conn = MySQLdb.connect(**self.__conn_dict)
        cur = conn.cursor()
        reCount = cur.execute(sql,params)
        data = cur.fetchall()
        conn.commit()
        cur.close()
        conn.close()
        return data
    def get_one(self,sql,params):
        conn = MySQLdb.connect(**self.__conn_dict)
        # * list ; ** dict
        # 没有加*，传入的是字典，加*后，传入的是字典的内容'host= ... object'
        cur = conn.cursor()
        reCount = cur.execute(sql,params)
        data = cur.fetchone()
        cur.close()
        conn.close()
        return data
